/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package wwg;

import java.awt.Color;
import java.lang.reflect.InvocationTargetException;
import java.util.Random;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;

/**
 *
 * @author CmptrPrgmr
 */
public class SplashScreen extends javax.swing.JFrame {
	
	/**
	 * Creates new form SplashScreen
	 */
	public SplashScreen() {
		initComponents();
		new progressBarLoader();
		new loadingText();
		//getContentPane().setBackground(new Color(35, 35, 35)); 
	}

	/**
	 * This method is called from within the constructor to initialize the form. WARNING: Do NOT
	 * modify this code. The content of this method is always regenerated by the Form Editor.
	 */
	@SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        SplashScreenPanel = new javax.swing.JPanel();
        SplashScreenPicturePanel = new javax.swing.JPanel();
        SplashScreenPictureLabel = new javax.swing.JLabel();
        loadingProgressBar = new javax.swing.JProgressBar();
        closeButton = new javax.swing.JButton();
        loadingLabel = new javax.swing.JLabel();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setPreferredSize(new java.awt.Dimension(720, 300));
        setResizable(false);
        setUndecorated(true);

        SplashScreenPanel.setBorder(javax.swing.BorderFactory.createEtchedBorder(javax.swing.border.EtchedBorder.RAISED));
        SplashScreenPanel.setOpaque(false);

        SplashScreenPicturePanel.setBorder(javax.swing.BorderFactory.createEtchedBorder(javax.swing.border.EtchedBorder.RAISED));

        SplashScreenPictureLabel.setIcon(new javax.swing.ImageIcon(getClass().getResource("/wwg/resources/World_Wide_Gmrz-WWG_Splash_Screen.png"))); // NOI18N

        javax.swing.GroupLayout SplashScreenPicturePanelLayout = new javax.swing.GroupLayout(SplashScreenPicturePanel);
        SplashScreenPicturePanel.setLayout(SplashScreenPicturePanelLayout);
        SplashScreenPicturePanelLayout.setHorizontalGroup(
            SplashScreenPicturePanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(SplashScreenPictureLabel)
        );
        SplashScreenPicturePanelLayout.setVerticalGroup(
            SplashScreenPicturePanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(SplashScreenPictureLabel)
        );

        loadingProgressBar.setStringPainted(true);

        closeButton.setText("Close");
        closeButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                closeButtonActionPerformed(evt);
            }
        });

        loadingLabel.setText("Loading");

        javax.swing.GroupLayout SplashScreenPanelLayout = new javax.swing.GroupLayout(SplashScreenPanel);
        SplashScreenPanel.setLayout(SplashScreenPanelLayout);
        SplashScreenPanelLayout.setHorizontalGroup(
            SplashScreenPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(SplashScreenPanelLayout.createSequentialGroup()
                .addComponent(SplashScreenPicturePanel, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(0, 0, Short.MAX_VALUE))
            .addGroup(SplashScreenPanelLayout.createSequentialGroup()
                .addContainerGap()
                .addComponent(loadingProgressBar, javax.swing.GroupLayout.PREFERRED_SIZE, 246, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(loadingLabel)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addComponent(closeButton)
                .addContainerGap())
        );
        SplashScreenPanelLayout.setVerticalGroup(
            SplashScreenPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(SplashScreenPanelLayout.createSequentialGroup()
                .addComponent(SplashScreenPicturePanel, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(SplashScreenPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(loadingProgressBar, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addGroup(SplashScreenPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(closeButton)
                        .addComponent(loadingLabel)))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(SplashScreenPanel, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(SplashScreenPanel, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
        );

        java.awt.Dimension screenSize = java.awt.Toolkit.getDefaultToolkit().getScreenSize();
        setBounds((screenSize.width-508)/2, (screenSize.height-328)/2, 508, 328);
    }// </editor-fold>//GEN-END:initComponents

	private class loadingText extends Thread
	{
		
		private loadingText()
		{
			Thread t = new Thread(this);
			t.start();
		}
		
		@Override
		public void run()
		{
			boolean reverse = false;
			while(loadingProgressBar.getValue() != 100)
			{
				if ("Loading".equals(loadingLabel.getText()))
				{
					loadingLabel.setText("Loading.");
					reverse = false;
				}
				else if (loadingLabel.getText().equals("Loading."))
				{
					if (reverse)
						loadingLabel.setText("Loading");
					else
						loadingLabel.setText("Loading..");
				}
				else if (loadingLabel.getText().equals("Loading.."))
				{
					if (reverse)
						loadingLabel.setText("Loading.");
					else
						loadingLabel.setText("Loading...");
				}
				else if (loadingLabel.getText().equals("Loading..."))
				{
					if (reverse)
						loadingLabel.setText("Loading..");
					else
						loadingLabel.setText("Loading....");
				}
				else if (loadingLabel.getText().equals("Loading...."))
				{
					if (reverse)
						loadingLabel.setText("Loading...");
					else
						loadingLabel.setText("Loading.....");
				}
				else if (loadingLabel.getText().equals("Loading....."))
				{
					if (reverse)
						loadingLabel.setText("Loading....");
					else
						loadingLabel.setText("Loading......");
				}
				else if (loadingLabel.getText().equals("Loading......"))
				{
					if (reverse)
						loadingLabel.setText("Loading.....");
					else
						loadingLabel.setText("Loading.......");
				}
				else if (loadingLabel.getText().equals("Loading......."))
				{
					if (reverse)
						loadingLabel.setText("Loading......");
					else
						loadingLabel.setText("Loading........");
				}
				else if (loadingLabel.getText().equals("Loading........"))
				{
					if (reverse)
						loadingLabel.setText("Loading.......");
					else
						loadingLabel.setText("Loading.........");
				}
				else if (loadingLabel.getText().equals("Loading........."))
				{
					if (reverse)
						loadingLabel.setText("Loading........");
					else
						loadingLabel.setText("Loading..........");
				}
				else if (loadingLabel.getText().equals("Loading.........."))
				{
					if (reverse)
						loadingLabel.setText("Loading........");
					reverse = true;
				}
				//Random random = new Random();
				try {
					Thread.sleep(100);//Long.parseLong(Integer.toString(random.nextInt(500) + 100)));
				} catch (InterruptedException ex) {
					Logger.getLogger(SplashScreen.class.getName()).log(Level.SEVERE, null, ex);
				}
			}
			loadingLabel.setText("FINISHED!");
			try {
				Thread.sleep(700);
			} catch (InterruptedException ex) {
				Logger.getLogger(SplashScreen.class.getName()).log(Level.SEVERE, null, ex);
			}
//			loadingLabel.setText("Loading apllication");
//			Random random = new Random();
//			for (int i = 0; i < random.nextInt(8) + 1; i++)
//			{
//				try {
//					Thread.sleep(100);
//				} catch (InterruptedException ex) {
//					Logger.getLogger(SplashScreen.class.getName()).log(Level.SEVERE, null, ex);
//				}
//				loadingLabel.setText(loadingLabel.getText() + ".");
//			}
			loadingLabel.setText("Starting application");
			setVisible(false);
			new MainApplication().setVisible(true);
		}
		
	}
	
	private class progressBarLoader extends Thread
	{
	
		private progressBarLoader()
		{
			Thread t = new Thread(this);
			t.start();
		}
		
		@Override
		public void run()
		{
			while(loadingProgressBar.getValue() != 100)
			{
				loadingProgressBar.setValue(loadingProgressBar.getValue() + 1);
				Random random = new Random();
				try {
					Thread.sleep(Long.parseLong(Integer.toString(random.nextInt(30) + 7)));
				} catch (InterruptedException ex) {
					Logger.getLogger(SplashScreen.class.getName()).log(Level.SEVERE, null, ex);
				}
			}
		}
		
	}
	
	private void closeButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_closeButtonActionPerformed
		dispose();
	}//GEN-LAST:event_closeButtonActionPerformed

	/**
	 * @param args the command line arguments
	 */
	public static void main(String args[]) {
		/*
		 * Set the Nimbus look and feel
		 */
		//<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /*
		 * If Nimbus (introduced in Java SE 6) is not available, stay with the default look and
		 * feel. For details see
		 * http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html
		 */
		try {
			UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
//			for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
//				if ("Nimbus".equals(info.getName())) {
//					javax.swing.UIManager.setLookAndFeel(info.getClassName());
//					break;
//				}
//			}
		} catch (ClassNotFoundException ex) {
			java.util.logging.Logger.getLogger(SplashScreen.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
		} catch (InstantiationException ex) {
			java.util.logging.Logger.getLogger(SplashScreen.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
		} catch (IllegalAccessException ex) {
			java.util.logging.Logger.getLogger(SplashScreen.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
		} catch (javax.swing.UnsupportedLookAndFeelException ex) {
			java.util.logging.Logger.getLogger(SplashScreen.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
		}
		//UIManager.put("ProgressBar.foreground", new Color(0, 0, 150));
		//</editor-fold>

		/*
		 * Create and display the form
		 */
		java.awt.EventQueue.invokeLater(new Runnable() {

			@Override
			public void run() {
				new SplashScreen().setVisible(true);
			}
		});
	}
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JPanel SplashScreenPanel;
    private javax.swing.JLabel SplashScreenPictureLabel;
    private javax.swing.JPanel SplashScreenPicturePanel;
    private javax.swing.JButton closeButton;
    private javax.swing.JLabel loadingLabel;
    private javax.swing.JProgressBar loadingProgressBar;
    // End of variables declaration//GEN-END:variables
}
